//
//  ViewController.m
//  Homework_24
//
//  Created by 徐昊 on 16/8/16.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "NSString+HXMD5.h"
#import "AFNetworking.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfEncryption;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOfDecryption;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)encrypt:(id)sender {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *parameters = [NSString stringWithFormat:@"text=%@", _textFieldOfEncryption.text];
    [session.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    [session POST:@"http://tool.zzblo.com/api/md5/encrypt" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"数据解析成功成功");
        NSString *str = [responseObject objectForKey:@"secret16"];
        _textFieldOfDecryption.text = str;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        _textFieldOfDecryption.text = @"数据解析失败";
    }];
}
- (IBAction)decrypt:(id)sender {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *parameters = [NSString stringWithFormat:@"secret=%@", _textFieldOfDecryption.text];
    [session.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
        return parameters;
    }];
    [session POST:@"http://tool.zzblo.com/api/md5/decrypt" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"数据解析成功");
        NSString *str = [responseObject objectForKey:@"text"];
        _textFieldOfEncryption.text = str;
        NSLog(@"%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        _textFieldOfEncryption.text = @"数据解析失败";
    }];
}
- (IBAction)changeValue:(id)sender {
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
